import React, { Dispatch, useEffect, SetStateAction } from 'react'
import ScreenWebShot from 'js-web-screen-shot'
import { positionInfoType } from 'js-web-screen-shot/dist/lib/type/ComponentType'
export default function WebScreenShot({
  base64,
  setBase64
}: {
  base64: string
  setBase64: Dispatch<SetStateAction<string>>
}): React.JSX.Element {
  useEffect(() => {
    new ScreenWebShot({
      imgSrc: base64, // 图片地址
      enableWebRtc: false, // 关闭webrtc
      level: 999, // 窗口层级
      wrcWindowMode: true, // 窗口截图模式
      writeBase64: true, // 写入剪贴板
      imgAutoFit: false, // 图片自动适应
      closeCallback: () => {
        console.log('截图结束')
        setBase64('')
      },
      triggerCallback: ({ code, msg }: { code: number; msg: string }) => {
        console.log('截图开始', code)
        console.log('msg', msg)
      },
      completeCallback: (imgInfo: { base64: string; cutInfo: positionInfoType }) => {
        console.log('截图绘制', imgInfo)
        window.electron.ipcRenderer.send('save-screenshot', imgInfo)
        setBase64('')
      },
      cancelCallback: () => {
        console.log('截图取消')
        setBase64('')
      },
      saveCallback: () => {
        console.log('截图保存')
        // 保存到本地，复制图片
        setBase64('')
      }
    })
  }, [])
  return (
    <>
      <div className="screen-shot bg-[transparent] !important"></div>
    </>
  )
}
